<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */

class CategoryAction extends Action{
    private $M_categorys;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_categorys = Model::getInstance('categorys');
    }

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $zh_name = $this->input()->post('zh_name');
        $filter = array();

        if($zh_name){
            $filter['zh_name'] = $zh_name;
        }

        $list =$this->M_categorys->getCategoryList($curr_page,$per_page,$filter);
        $data = array(
            'list'=>$list,
        );
        Template::show('Category/index.php',$data);
    }

    public function getCategorys($filter = array(),$flag = false){
        return $this->M_categorys->getCategorys($filter,$flag);
    }

    public function add(){

        Template::show('Category/add.php');
    }

    public function del(){

        $category_id = $this->input()->get('category_id');
        $ret = $this->M_categorys->delCategory($category_id);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"category",
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    public function edit(){

        $category_id = (int)$this->input()->get('category_id');

        $category_info = $this->M_categorys->get($category_id);
        $data = array(
            'category_info'=>$category_info,
        );

        Template::show('Category/edit.php',$data);
    }

    public function ajaxAdd(){

        $en_name = $this->input()->post('en_name');
        $zh_name = $this->input()->post('zh_name');

        if(!$zh_name || !$en_name){
            $data = array(
                "statusCode"=>"300",
                "message"=>"必须填写英文名及中文名",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_categorys->addCategory($en_name, $zh_name);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"添加成功",
                "navTabId"=>"category",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    public function ajaxEdit(){

        $category_id = $this->input()->post('category_id');
        $en_name = $this->input()->post('en_name');
        $zh_name = $this->input()->post('zh_name');

        if(!$category_id || (!$zh_name && !$en_name)){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_categorys->updateCategory($category_id,$en_name,$zh_name);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"修改成功",
                "navTabId"=>"category",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"修改失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }
}